﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyDelegate
{
    /// <summary>
    /// 定义事件--实现发布订阅
    /// 
    /// 1.朝夕要发布新课程，嵌入式和C++ Qt (都是Richard老师给大家上课~~)
    /// 2.很多小伙伴们很期待，很关注
    /// 3.如果课程发布，根据需求报名学习~~
    /// 
    /// </summary>
    public class EventStandard
    {
        private static PublicSource publicCourse1 = new PublicSource()
        {
            Id = 222,
            Name = "Qt和C++",
        };
        private static PublicSource publicCourse2 = new PublicSource()
        {
            Id = 222,
            Name = "嵌入式开发",
        };

        /// <summary>
        /// 发布课程
        /// </summary>
        public static void Show()
        {
            publicCourse1.PublicShow();// 只关注发布，发布后，会有后续的行为
            publicCourse2.PublicShow();
        }

        public static void Init()
        {
            StudentUser user1 = new StudentUser()
            {
                Id = 123,
                Name = "夕林吹雪"
            };
            publicCourse1.Publish += user1.Buy;
            publicCourse2.Publish += user1.Buy;

            StudentUser user2 = new StudentUser()
            {
                Id = 123,
                Name = "张三"
            };
            publicCourse1.Publish += user2.Buy;
            publicCourse2.Publish += user2.Buy;


            StudentUser user3 = new StudentUser()
            {
                Id = 123,
                Name = "李四"
            };
            publicCourse1.Publish += user3.Buy;
            publicCourse2.Publish += user3.Buy;
        }
    }

    /// <summary>
    /// 发布者
    /// 发布后，就会有后续的逻辑---具体是什么，不知道 
    /// </summary>
    public class PublicSource
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public void PublicShow()
        {
            Console.WriteLine("朝夕新课程发布： Qt+C++  嵌入式开发~~~");
            //会有很多的人关注
            //会有很多的后续动作执行， 有人咨询，有人报名，有人体验课程~

            Publish.Invoke(this, new SourceInfo()
            {
                Id = 345,
                Title = "Qt和 C++"
            });
        }

        public event EventHandler Publish;
    }

    /// <summary>
    /// 订阅者（订户）：
    /// 订阅消息，消息出现，就会触发行为；
    /// </summary>
    public class StudentUser
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public void Buy(object? sender, EventArgs e)
        {
            PublicSource course = (PublicSource)(sender);
            Console.WriteLine($"朝夕有新的课程发布，可称为{course.Name}");
            Console.WriteLine($"用户:{Name}先了解下，考虑考虑");
            Console.WriteLine($"可以购买学习~~");
        }

    }

    /// <summary>
    /// 扩展参数
    /// </summary>
    public class SourceInfo : EventArgs
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string TeacherWechatNum { get; set; }
    }
}
